/*
 * Button.java  0.4.0 / Oct 25, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing;

import com.cute4j.swing.properties.ButtonProperties;

/**
 * Button Component.
 *
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class Button extends AbstractButton implements ButtonProperties {

// Private Static Fields. ------------------------------------------------------

  private static final Object CLASS_LOCK;
  private static boolean useAutoDefaultButtons;

// Static Initialization. ------------------------------------------------------

  static {
    CLASS_LOCK = new Object();
  }

// Private Fields. -------------------------------------------------------------

  private boolean defaultCapable;
  private boolean autoDefault;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new Button object.
   */
  public Button() {
    this(null, null);
  }

  /**
   * Constructs new AbstractButton object.
   *
   * @param action the action
   */
  public Button(Action action) {
    this(null, null);
    setAction(action); // NOPMD
  }

  /**
   * Constructs new Button object.
   *
   * @param text the button text
   */
  public Button(String text) {
    this(text, null);
  }

  /**
   * Constructs new Button object.
   *
   * @param icon the icon
   */
  public Button(Icon icon) {
    this(null, icon);
  }

  /**
   * Constructs new Button object.
   *
   * @param text the button text
   * @param icon the icon
   */
  public Button(String text, Icon icon) {
    super(text, icon);
    synchronized (CLASS_LOCK) {
      if (useAutoDefaultButtons) {
        autoDefault = true;
        _setAutoDefault(_delegate, true); // NOPMD
      }
    }
  }

// Public Static Methods. ------------------------------------------------------

  /**
   * Returns <tt>true</tt> if new buttons are auto default.
   *
   * @return <tt>true</tt> if new buttons are auto default
   */
  public static boolean isUsingAutoDefaultButtons() {
    synchronized (CLASS_LOCK) {
      return useAutoDefaultButtons;
    }
  }

  /**
   * Sets whether or not new buttons are auto default.
   *
   * @param useAutoDefaultButtons if <tt>true</tt> new buttons are auto default
   */
  public static void setUseAutoDefaultButtons(boolean useAutoDefaultButtons) {
    synchronized (CLASS_LOCK) {
      Button.useAutoDefaultButtons = useAutoDefaultButtons;
    }
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns default capable status.
   *
   * @return <tt>true</tt> if the button is default
   */
  public boolean isDefaultCapable() {
    if (autoDefault) {
      disposedCheck();
      return _isDefault(_delegate);
    }
    return defaultCapable;
  }

  /**
   * Sets button's <tt>defaultCapable</tt> property.
   *
   * @param defaultCapable if <tt>true</tt> this button will be the default
   *                       button
   */
  public void setDefaultCapable(boolean defaultCapable) {
    disposedCheck();
    boolean oldDefaultCapable;
    if (autoDefault) {
      oldDefaultCapable = _isDefault(_delegate);
    } else {
      oldDefaultCapable = this.defaultCapable;
    }
    if (oldDefaultCapable != defaultCapable) {
      _setDefaultCapable(_delegate, defaultCapable);
      this.defaultCapable = defaultCapable;
      firePropertyChange(PROPERTY_DEFAULT_CAPABLE, oldDefaultCapable,
                         defaultCapable);
    }
  }

  /**
   * Returns button's auto default status.
   *
   * @return auto default status
   */
  public boolean isAutoDefault() {
    return autoDefault;
  }

  /**
   * Sets button's <tt>autoDefault</tt> property.
   *
   * @param autoDefault if <tt>true</tt> the button automatically changes its
   *                    default state.
   */
  public void setAutoDefault(boolean autoDefault) {
    boolean oldAutoDefault = this.autoDefault;
    if (oldAutoDefault != autoDefault) {
      disposedCheck();
      _setAutoDefault(_delegate, autoDefault);
      this.autoDefault = autoDefault;
      firePropertyChange(PROPERTY_AUTO_DEFAULT, oldAutoDefault, autoDefault);
    }
  }

// Package-Private Native Method. ----------------------------------------------

  native long _create();

  native boolean _isDefault(long delegate);

  native void _setDefaultCapable(long delegate, boolean defaultCapable);

  native void _setAutoDefault(long delegate, boolean autoDefault);

}
